<!--2018/12/23 21:13. by soft-->
<link rel="stylesheet" href="${root}/static/layui/css/layui.css">
<script src="${root}/static/layui/layui.js"></script>

<div style="padding: 25px 25px 0 0;">
  <form class="layui-form" action="">
    <input type="hidden" name="id" value="${dept.id!''}">
    <div class="layui-form-item">
      <label class="layui-form-label">所属学院</label>
      <div class="layui-input-block">
        <select id="collage" name="collageId" lay-filter="collageF">
          <option>默认</option>
        </select>
      </div>
    </div>
    <div class="layui-form-item">
      <label class="layui-form-label">部门名</label>
      <div class="layui-input-block">
        <input name="name" value="${dept.name!''}" lay-verType="tips" lay-verify="required"
               placeholder="请输入部门名" class="layui-input">
      </div>
    </div>
    <div class="layui-form-item layui-form-text">
      <label class="layui-form-label">部门简介</label>
      <div class="layui-input-block">
        <textarea name="description" placeholder="请输入部门简介" style="resize: none;"
                  lay-verType="tips" lay-verify="required" class="layui-textarea">${dept.description!''}</textarea>
      </div>
    </div>
    <div class="layui-form-item">
      <div class="layui-input-block" style="text-align: right;">
        <#if type=='add'>
          <button class="layui-btn layui-btn-sm layui-btn-normal" lay-submit lay-filter="add_">添加</button>
        <#elseif type=='change'>
          <button class="layui-btn layui-btn-sm layui-btn-warm" lay-submit lay-filter="change_">修改</button>
        </#if>
      </div>
    </div>
  </form>

  <script>
    layui.use(['form', 'jquery', 'layer'], function(){
      var collegeId = '${dept.collageId!0}';
      var form = layui.form;
      var $ = layui.$;
      var layer = layui.layer;

      var collage = $("#collage");
      // 设置学院
      $.post('${root}/collage/list', function (data) {
        collage.html('');
        if (data.status === 200) {
          var items = data.payload;
          for (var i = 0; i < items.length; i++) {
            var item = items[i];
            var s = '';
            if (collegeId == item.id) {
              s = "selected ";
            }
            var opt = '<option ' + s + ' value="' + item.id + '">' + item.name + '</option>';
            collage.append(opt);
          }
        } else {
          layer.alert('网络异常！', {
            icon: 5
          }, function (idx) {
            layer.closeAll();
          });
        }
        form.render('select');
      });

      <#if type=='add'> // 添加
      form.on('submit(add_)', function(item) {
        $.post('${root}/dept/add', item.field, function (data) {
          if (data.status === 200) {
            setTimeout(function () {
              var idx = window.parent.layer.getFrameIndex(window.name);
              window.parent.layer.close(idx);
              window.parent.table.reload('depts'); // 表格重载
            }, 1050);
            layer.msg("添加成功！", {icon: 6, time: 1000});
          } else {
            layer.alert(data.message, {icon: 2, title: '添加失败'}, function () {
              layer.closeAll();
            });
          }
        });
        return false;
      });
      </#if>

      <#if type=='change'> // 修改
      form.on('submit(change_)', function(item) {
        $.post('${root}/dept/change', item.field, function (data) {
          if (data.status === 200) {
            setTimeout(function () {
              var idx = window.parent.layer.getFrameIndex(window.name);
              window.parent.layer.close(idx);
              window.parent.table.reload('depts'); // 表格重载
            }, 1050);
            layer.msg("修改成功！", {icon: 6, time: 1000});
          } else {
            layer.alert(data.message, {icon: 2, title: '修改失败'}, function () {
              layer.closeAll();
            });
          }
        });
        return false;
      });
      </#if>
    });
  </script>
</div>